package com.situ.stl.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.stl.Service.CourseService;
import com.situ.stl.dao.CourseMapper;
import com.situ.stl.model.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    CourseMapper courseMapper;

    @Autowired
    public void setCourseMapper(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }

    @Override
    public Page<Course> findAllWithCoachName(Page<Course> page, Course course) {
        // 构造查询条件，使用表别名明确字段所属表
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();

        // 课程名称：指定课程表别名c，避免与教练表name冲突
        if (course.getName() != null && !course.getName().trim().isEmpty()) {
            queryWrapper.like("c.name", course.getName().trim());
        }

        // 教练姓名：使用教练表别名co，匹配XML中的coach_name别名
        if (course.getCoachName() != null && !course.getCoachName().trim().isEmpty()) {
            queryWrapper.like("co.name", course.getCoachName().trim());
        }

        // 执行查询：参数为分页对象和条件对象，与Mapper接口匹配
        return courseMapper.selectCourseWithCoachName(page, queryWrapper);
    }

    @Override
    public List<Course> getByIds(List<Long> ids) {
        return courseMapper.selectBatchIds(ids);
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        // 错误：BaseMapper没有deleteByIds方法，改为deleteBatchIds
        return courseMapper.deleteBatchIds(ids);
    }


    //保存
    @Override
    public boolean save(Course course) {
        return courseMapper.insert(course) == 1;
    }

    //更新
    @Override
    public boolean update(Course course) {
        return courseMapper.updateById(course) == 1;
    }

}
